package com.vmladenov.cook.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.vmladenov.cook.R;
import com.vmladenov.cook.core.AppSettings;

public class SettingsActivity extends Activity
{
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		LoadPreferences();
		CheckBox checkBox1 = (CheckBox) findViewById(R.id.cbDownloadImages);
		checkBox1.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
			{
				CheckBox checkBox2 = (CheckBox) findViewById(R.id.cbCacheImages);
				if (isChecked)
				{
					checkBox2.setEnabled(true);
				}
				else
				{
					checkBox2.setChecked(false);
					checkBox2.setEnabled(false);
				}
			}
		});
	}

	private void LoadPreferences()
	{
		SharedPreferences preferences = getSharedPreferences("MandjaSettings", MODE_PRIVATE);
		
		AppSettings settings = new AppSettings(preferences);
		CheckBox checkBox2 = (CheckBox) findViewById(R.id.cbCacheImages);
		checkBox2.setChecked(settings.cacheImages());
		
		CheckBox checkBox1 = (CheckBox) findViewById(R.id.cbDownloadImages);
		checkBox1.setChecked(settings.downloadImages());
		
		CheckBox checkBox3 = (CheckBox) findViewById(R.id.cbUseOnlyWiFi);
		checkBox3.setChecked(settings.useOnlyWiFi());
	}

	public void onSavePreferences(View v)
	{
		SharedPreferences preferences = getSharedPreferences("MandjaSettings", MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();
		
		CheckBox checkBox1 = (CheckBox) findViewById(R.id.cbDownloadImages);
		editor.putBoolean(AppSettings.DownloadImagesField, checkBox1.isChecked());
		
		CheckBox checkBox2 = (CheckBox) findViewById(R.id.cbCacheImages);
		editor.putBoolean(AppSettings.CacheImagesField, checkBox2.isChecked());
		
		CheckBox checkBox3 = (CheckBox) findViewById(R.id.cbUseOnlyWiFi);
		editor.putBoolean(AppSettings.UseOnlyWiFiField, checkBox3.isChecked());
		
		editor.commit();

		Intent intent = new Intent();
		intent.setClass(SettingsActivity.this, MainActivity.class);
		startActivity(intent);
	}
}